package fairygui.editor.publish.exporter
{
   import fairygui.editor.publish.PublishStep;
   import fairygui.editor.utils.UtilsFile;
   import fairygui.editor.utils.UtilsStr;
   import flash.filesystem.File;
   import flash.utils.ByteArray;
   import mx.utils.Base64Encoder;
   
   public class LayaExporter extends PublishStep
   {
       
      
      public function LayaExporter()
      {
         super();
      }
      
      override public function run() : void
      {
         var ba:ByteArray = null;
         var curr:int = 0;
         var size:int = 0;
         var pos:int = 0;
         var fn:String = null;
         var files:Array = null;
         var checkStr:String = null;
         var i:int = 0;
         var nn:String = null;
         var file:File = null;
         var descData:* = undefined;
         var str:String = null;
         var enc:Base64Encoder = null;
         var descFile:File = new File(publishData.filePath + "/" + publishData.fileName + "." + publishData.fileExtention);
         var resPrefix:String = publishData.filePath + "/" + publishData.fileName + "@";
         if(publishData.sprites == null)
         {
            try
            {
               ba = UtilsFile.loadBytes(descFile);
               ba.inflate();
               str = ba.readUTFBytes(ba.length);
               curr = 0;
               while(true)
               {
                  pos = str.indexOf("|",curr);
                  if(pos == -1)
                  {
                     break;
                  }
                  fn = str.substring(curr,pos);
                  curr = pos + 1;
                  pos = str.indexOf("|",curr);
                  size = parseInt(str.substring(curr,pos));
                  curr = pos + 1;
                  if(fn == "sprites.bytes")
                  {
                     publishData.sprites = str.substr(curr,size);
                     break;
                  }
                  curr = curr + size;
               }
            }
            catch(err:Error)
            {
               stepCallback.addMsg("Unable to publish desc only, try publish all!");
               stepCallback.callOnFail();
               return;
            }
         }
         if(!publishData.exportDescOnly)
         {
            files = descFile.parent.getDirectoryListing();
            checkStr = publishData.fileName + "@";
            i = 0;
            while(i < files.length)
            {
               nn = files[i].name;
               if(UtilsStr.startsWith(nn,checkStr))
               {
                  try
                  {
                     files[i].deleteFile();
                  }
                  catch(err:Error)
                  {
                     stepCallback.addMsg("Unable to delete file \'" + nn + "\'");
                     stepCallback.callOnFailImmediately();
                     return;
                  }
               }
               i++;
            }
            for(fn in publishData.outputRes)
            {
               file = new File(resPrefix + fn);
               UtilsFile.saveBytes(file,publishData.outputRes[fn]);
            }
         }
         var output:Array = [];
         var toSort:Array = [];
         for(fn in publishData.outputDesc)
         {
            toSort.push(fn);
         }
         toSort.sort();
         for each(fn in toSort)
         {
            descData = publishData.outputDesc[fn];
            if(descData is XML)
            {
               str = (descData as XML).toXMLString();
            }
            else
            {
               str = String(descData);
            }
            output.push(fn);
            output.push("|");
            output.push("" + str.length);
            output.push("|");
            output.push(str);
         }
         output.push("sprites.bytes");
         output.push("|");
         output.push("" + publishData.sprites.length);
         output.push("|");
         output.push(publishData.sprites);
         if(publishData.hitTestData.length > 0)
         {
            enc = new Base64Encoder();
            enc.encodeBytes(publishData.hitTestData);
            str = enc.toString().replace(/[\r\n]/g,"");
            output.push("hittest.bytes");
            output.push("|");
            output.push("" + str.length);
            output.push("|");
            output.push(str);
         }
         ba = new ByteArray();
         ba.writeUTFBytes(output.join(""));
         ba.deflate();
         UtilsFile.saveBytes(descFile,ba);
         stepCallback.callOnSuccess();
      }
   }
}
